#!/usr/bin/python
# -*- coding: UTF-8 -*-

import xlrd
from Scripts.Common import Map
from Scripts.Startup import StartupConfigData
from Scripts.Excel import ExcelConfigLineData
from Scripts.Excel import ExcelConfigData

#
# Loads all of the excels data
# @param [configDatas] the config datas
#
def Load(configDatas):
    if None == configDatas:
        return
    
    excelConfigDatas = []

    #it reads all of excels in turn
    for i in range(len(configDatas)):
        configData = configDatas[i]
        excelUrl = configData.GetExcelUrl()
        
        #it reads a excel
        excelDocument = _DoLoadExcel(excelUrl)
        if None == excelDocument:
            continue

        #it reads a sheet in a excel
        excelSheet = configData.GetExcelSheet()
        excelSheetTable = excelDocument.sheet_by_name(excelSheet)
        if None == excelSheetTable:
            continue
        
        rowCount = excelSheetTable.nrows
        if 0 == rowCount:
            continue

        rowTitleElement = excelSheetTable.row_values(0)
        if None == rowTitleElement:
            continue

        #it reads all of the titles of a excel
        excelTitleMap = _DoReadExcelTitle(rowTitleElement)
        if None == excelTitleMap:
            continue

        dataClassify = configData.GetClassify()
        excelOutputName = configData.GetOutputName()
        excelConfigData = ExcelConfigData.Class(dataClassify, excelOutputName)

        #it reads all of the line in a excel in turn
        for rowIndex in range(1, rowCount):
            rowElement = excelSheetTable.row_values(rowIndex)
            if rowElement != None:
                rowData = ExcelConfigLineData.Class()
                
                configColumnCount = configData.GetColumnCount()
                for configColumnIndex in range(configColumnCount):
                    configColumnData = configData.GetColumnData(configColumnIndex)
                    configDataName = configColumnData.GetDataName()
                    isExist = excelTitleMap.ExistData(configDataName)
                    if True == isExist:
                        configOutputName = configColumnData.GetOutputName()
                        configDataType = configColumnData.GetDataType()
                        titleIndex = excelTitleMap.FindData(configDataName)
                        dataValue = rowElement[titleIndex]
                        rowData.AddData(configOutputName, configDataType, dataValue)
                
                isRowEmpty = rowData.IsEmpty()
                if False == isRowEmpty:
                    excelConfigData.AddLineData(rowData)
        excelConfigDatas.append(excelConfigData)
    return excelConfigDatas

#
# the action of loading a excel
# @param [excelUrl] the excel url
#      
def _DoLoadExcel(excelUrl):
    try:
        excelDocument = xlrd.open_workbook(excelUrl)
        return excelDocument
    except Exception as err:
        print(err)
        return None

#
# Reads a title of the excel
# @param [rowTitleElement] the title row element
# @return title container
#
def _DoReadExcelTitle(rowTitleElement):
    excelTitleMap = Map.Class()
    for i in range(len(rowTitleElement)):
        rowTitleName = rowTitleElement[i]
        excelTitleMap.SetData(rowTitleName, i)
    return excelTitleMap